package com.mef.smsplat.web.controller.sys.user;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hc24.util.IDUtil;
import com.mef.smsplat.dao.base.DataGridModel;
import com.mef.smsplat.dao.base.Page;
import com.mef.smsplat.domain.RoleInfoDomain;
import com.mef.smsplat.exception.BusinessException;
import com.mef.smsplat.service.RoleInfoService;
import com.mef.smsplat.util.Constants;
import com.mef.smsplat.web.controller.base.Json;

@Controller
@RequestMapping("/sys/role")
public class RoleInfoController {
	private RoleInfoService roleInfoService;

	public void setRoleInfoService(RoleInfoService roleInfoService) {
		this.roleInfoService = roleInfoService;
	}
	
	@ResponseBody
	@RequestMapping("/query.sys")
	public Page<RoleInfoDomain> query(String roleName,DataGridModel dgm){
		return roleInfoService.query(roleName, dgm);
	}
	
	@ResponseBody
	@RequestMapping("/queryAll.sys")
	public List<RoleInfoDomain> queryAll(){
		return roleInfoService.queryAll();
	}
	
	@ResponseBody
	@RequestMapping("/add.sys")
	public Json add(RoleInfoDomain role){
		if(role.getRoleName()==null || "".equals(role.getRoleName().trim())){
			throw new BusinessException("角色名不能为空");
		}
		if(!roleInfoService.isUnique(role.getRoleName(), role.getRoleId())){
			throw new BusinessException("此角色名已经存在");
		}
		role.setRoleId(IDUtil.next()+"");
		role.setCreateTime(new Date());
		role.setStatus(Constants.NORMAL);
		roleInfoService.save(role);
		return new Json();
	}
	
	@ResponseBody
	@RequestMapping("/update.sys")
	public Json update(RoleInfoDomain role){
		if(role.getRoleName()==null || "".equals(role.getRoleName().trim())){
			throw new BusinessException("角色名不能为空");
		}
		if(!roleInfoService.isUnique(role.getRoleName(), role.getRoleId())){
			throw new BusinessException("此角色名已经存在");
		}
		roleInfoService.update(role);
		return new Json();
	}
	
	@ResponseBody
	@RequestMapping("/del.sys")
	public Json del(String[] ids){
		roleInfoService.removeById(ids);
		return new Json();
	}
	
	@ResponseBody
	@RequestMapping("/enable.sys")
	public Json enable(String[] ids){
		roleInfoService.enable(ids);
		return new Json();
	}
	
	@ResponseBody
	@RequestMapping("/disable.sys")
	public Json disable(String[] ids){
		roleInfoService.disable(ids);
		return new Json();
	}
	
}
